package com.zxcl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxcl.constant.BusinessEnum;
import com.zxcl.domain.IndexImg;
import com.zxcl.domain.Prod;
import com.zxcl.dto.PageDTO;
import com.zxcl.ex.BusinessException;
import com.zxcl.feign.StoreProdFeign;
import com.zxcl.mapper.IndexImgMapper;
import com.zxcl.model.Result;
import com.zxcl.service.IndexImgService;
import com.zxcl.utils.AuthUtil;
import com.zxcl.vo.IndexImgVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * 描述: []
 * 作者: zxcl
 */
@Service
public class IndexImgServiceImpl extends ServiceImpl<IndexImgMapper, IndexImg> implements IndexImgService {

    @Autowired
    private IndexImgMapper indexImgMapper;

    @Autowired
    private StoreProdFeign storeProdFeign;



    /**
     * 分页查询轮播图
     *
     * @param pageDTO
     * @param status
     * @return
     */
    @Override
    public Page<IndexImg> loadIndexImgPage(PageDTO pageDTO, Integer status) {
        Page<IndexImg> page = new Page<>(pageDTO.getCurrent(), pageDTO.getSize());
        Long shopId = AuthUtil.getShopId();
        return indexImgMapper.selectPage(page, new LambdaQueryWrapper<IndexImg>()
                .eq(!shopId.equals(1L), IndexImg::getShopId, shopId)
                .eq(!ObjectUtils.isEmpty(status), IndexImg::getStatus, status)
                .orderByDesc(IndexImg::getSeq)
        );
    }

    /**
     * 新增轮播图
     * @param indexImg
     * @return
     */
    @Override
    public Integer addIndexImg(IndexImg indexImg) {
        Long shopId = AuthUtil.getShopId();
        indexImg.setShopId(shopId);
        indexImg.setCreateTime(new Date());
        return indexImgMapper.insert(indexImg);
    }


    /**
     * 回显轮播图
     *
     * @param id
     * @return
     */
    @Override
    public IndexImgVO findIndexImg(Long id) {
        if (ObjectUtils.isEmpty(id)) {
            throw new BusinessException("轮播图编号不能为空");
        }
        //根据id查询轮播图数据
        IndexImg indexImg = getBaseMapper().selectById(id);
        //取出商品编号
        Long prodId = indexImg.getProdId();
        if (ObjectUtils.isEmpty(prodId)) {
            throw new BusinessException("当前轮播图没有关联任何商品");
        }
        //返回商品信息
        Result<Prod> prodRes = storeProdFeign.getProdById(prodId);
        if (prodRes.getCode().equals(BusinessEnum.SERVER_INNER_ERROR.getCode())){
            throw new BusinessException("远程调用失败");
        }
        if (prodRes.getCode().equals(BusinessEnum.UN_AUTHORIZATION.getCode())){
            throw new BusinessException("没有携带token");
        }
        Prod prod = prodRes.getData();
        IndexImgVO indexImgVO = new IndexImgVO();
        BeanUtils.copyProperties(indexImg,indexImgVO);
        indexImgVO.setPic(prod.getPic());
        indexImgVO.setProdName(prod.getProdName());
        return indexImgVO;
    }


    /**
     * 加载商城首页轮播图
     * @return
     */
    @Override
    public List<IndexImg> findMallIndexImg() {

        return getBaseMapper().selectList(new LambdaQueryWrapper<IndexImg>()
                .select(IndexImg::getImgUrl,IndexImg::getProdId)
                .eq(IndexImg::getStatus,1)
                .orderByDesc(IndexImg::getSeq)
        );
    }

}
